package com.guodu.dao.impl;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.aspectj.weaver.ast.Not;
import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.NoticemanagerDao;
import com.guodu.entity.FileTechnical;
import com.guodu.entity.Filenotice;
import com.guodu.entity.Noticemanager;
import com.guodu.entity.NoticemanagerUnits;
import com.guodu.entity.Units;
import com.guodu.vo.NoticemanagerQueryVo;


@Repository("noticemanagerDao")
public class NoticemanagerDaoImpl extends BaseDao implements NoticemanagerDao {

	@Override
	public long findSizeByVo(NoticemanagerQueryVo nqv, Units units,String fUnitName) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from Noticemanager m where 1=1");
		
		if(nqv!=null){
			if(nqv.getCreateBeginTime()!=null&&nqv.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+nqv.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(nqv.getCreateEndTime()!=null&&nqv.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+nqv.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
		}
		
		if(units==null){
			hql.append("");
		}else if(units.getUnitsLevel()==1L){
			hql.append(" and m.unitsNamezu like '%"+units.getUnitsId()+"%' or exists(select 1 from UserInfo f where f.userId = m.createPeople and f.unitsId  ="+units.getUnitsId()+" )");
		}else if(units.getUnitsLevel()==2L){
			//hql.append(" and m.unitsNamezu like '%"+units.getUnitsName()+"%'");
					
			hql.append(" and (m.unitsNamezu like '%"+units.getUnitsId()+"%' or m.unitsNamezu like '%"+units.getFunitsId()+"%')");
			
		}
		
		return super.findInt(hql.toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Noticemanager> findByVo(NoticemanagerQueryVo nqv, Units units,
			int curPage, int pageSize,String fUnitName) {
		// TODO Auto-generated method stub
		
		StringBuffer hql = new StringBuffer("from Noticemanager m where 1=1 ");
		
		if(nqv!=null){
			if(nqv.getCreateBeginTime()!=null&&nqv.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+nqv.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(nqv.getCreateEndTime()!=null&&nqv.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+nqv.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
		}
		
		if(units==null){
			hql.append("");
		}else if(units.getUnitsLevel()==1L){
			hql.append(" and m.unitsNamezu like '%"+units.getUnitsId()+"%' or exists(select 1 from UserInfo f where f.userId = m.createPeople and f.unitsId  ="+units.getUnitsId()+" )");
		}else if(units.getUnitsLevel()==2L){
			//hql.append(" and m.unitsNamezu like '%"+units.getUnitsName()+"%'");
					
			hql.append(" and (m.unitsNamezu like '%"+units.getUnitsId()+"%' or m.unitsNamezu like '%"+units.getFunitsId()+"%')");
			
		}
		hql.append( " order by noticeId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public String save(Noticemanager noticemanager) {
		// TODO Auto-generated method stub
		super.save(noticemanager);
		List<Noticemanager> notices=super.findByProperty(Noticemanager.class, "noticeFile", noticemanager.getNoticeFile());
		Noticemanager notice = new Noticemanager();	
		if(notices.size()>0){
			notice=notices.get(0);
		}
		return notice.getNoticeId();
		
	}

	@Override
	public Noticemanager findById(String purchaseId) {
		// TODO Auto-generated method stub
		return (Noticemanager) super.findById(Noticemanager.class, purchaseId);
	}

	@Override
	public void saveFile(Filenotice filenotice) {
		// TODO Auto-generated method stub
		super.save(filenotice);
	}

	@Override
	public void delZi(String noticeFile) {
		// TODO Auto-generated method stub
		List<Filenotice> filenotice = this.findFile(noticeFile);
		for (Filenotice filenotice2 : filenotice) {
			String path = ServletActionContext.getServletContext().getRealPath("/")+ "/upload/Noticemanager/";
			File file = new File(path+filenotice2.getFileRealname());
			if (file.isFile() && file.exists()) {  
				file.delete();
			}
			delete(FileTechnical.class, filenotice2.getFilenoticeId());
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Filenotice> findFile(String purchaseId) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer(" from Filenotice f where f.noticeId = '"+purchaseId+"'");
		return (List<Filenotice>) super.find(hql.toString());
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		super.deleteAllById(Noticemanager.class, delIds);
		
		for(int i =0;i<delIds.length;i++){
			String hql = "delete from NoticemanagerUnits where noticemanagerId='"+delIds[i]+"'";
			super.executeUpdate(hql);
		}

	}

	@Override
	public Noticemanager findByUnits(String unitsId) {
		// TODO Auto-generated method stub
		Noticemanager n = new Noticemanager();
		String hql = "select noticemanagerId from NoticemanagerUnits where unitsId='"+unitsId+"' and status=1";
		List list = super.find(hql);
		if(list.size()>0){
			String id = list.get(0).toString();
			return (Noticemanager) super.findById(Noticemanager.class, id);
		}else{
		return null;
		}
	}

	@Override
	public void updateNotice(Units units, String purchaseId) {
		// TODO Auto-generated method stub
		if(units!=null){
		String hql = "update NoticemanagerUnits set status=0 where noticemanagerId='"+purchaseId+"' and status=1 and unitsId='"+units.getUnitsId()+"'";
		super.executeUpdate(hql);
		}
	}

	@Override
	public void saveNoticeUnits(List<NoticemanagerUnits> nus) {
		// TODO Auto-generated method stub
		super.saveAll(nus);	
	}

}
